$(function(){
    $('.beforeajax').css({
        left:($(window).width()-80)/2
    })
    //点击删除
    $('.delarc').live('click',function(){
        if(!window.confirm('确定删除？')){
            return false;
        }
        var aid = $(this).attr('name');
        var _this = $(this);
        $.get(url+'/delarc/aid/'+aid,function(str){
            if(str.num==1){
                _this.parent().parent().remove();
                location.href = location.href;
                alert('删除成功')
            }else{
                alert('删除失败')
            }
        },'json')
    })
    //点击全选的时候
    $('.find_all').click(function(){
        $('input').attr('checked',true);
    })
    //点击全不选
    $('.not_all').click(function(){
        $('input').attr('checked',false);
    })
    //点击反选
    $(".find_other").click(function(){
        $('input').each(function(){
            $(this).attr('checked',!$(this).attr('checked'))
        })
    })
    //点击删除
    $('.del').click(function(){
        var stop=true;
        $(":checkbox").each(function(){
            if($(this).attr('checked')){
                stop=true;
                return false;
            }else{
                stop=false;
            }
        })
        if(stop){
            if(!window.confirm('确定删除？')){
                return false;
            }
            var aid = '';
            $(':checkbox:checked').each(function(){
                aid +=$(this).val()+'-';
            })
            $.get(url+'/delarc/aid/'+aid,function(str){
                if(str.num==1){
                    $(':checkbox:checked').each(function(){
                        $(this).parent().parent().remove();
                    })
                    location.href = location.href;
                    alert('删除成功');
                }else{
                    alert('删除失败')
                }
            },'json')
        }
    }) 
    //图片旋转js
    var circle = new Sonic({

        width: 50,
        height: 50,
        padding: 50,

        strokeColor: '#000',

        pointDistance: .01,
        stepsPerFrame: 3,
        trailLength: .7,

        step: 'fader',

        setup: function() {
            this._.lineWidth = 5;
        },

        path: [
        ['arc', 25, 25, 25, 0, 360]
        ]

    });
    circle.play();
  
    //页面载如完成后 用ajax 载入数据
    startajax(1,0);
    //点击上下页
    var stop = 1;
    $('.next , .pre').live('click',function(){
        var href = $(this).attr('href');
        var reg = /\/page\/(\d+)/ig;
        if(reg.exec(href)){
            var num = RegExp.$1;
        }
        $(this).attr('href',"#");
        $('.arc, .page').remove();
        $('.beforeajax').append(circle.canvas);
        $('.beforeajax').show();
        switch(stop){
            case 1:
                startajax(num,0); 
                break;
            case 2:
                var main = $('.time').attr('main');
                if(main==1){
                    main=2;
                }else{
                    main=1;
                }
                startajax(num,main);
                break;
            case 3:
                var main = $('.click').attr('main');
                if(main==3){
                    main=4;
                }else{
                    main=3;
                }
                startajax(num,main);
                break;
        }    
    })
    //点击点击数排列
    $('.click').toggle(function(){
        stop = 3;
        $(this).html('点击数↑');
        var main =  $(this).attr('main');
        $('.arc, .page').remove();
        $('.beforeajax').show();
        startajax(1,main);
        $(this).attr('main',4);
        
    },function(){
        stop = 3;
        $(this).html('点击数↓');
        var main =  $(this).attr('main');
        $('.arc, .page').remove();
        $('.beforeajax').show();
        startajax(1,main);
        $(this).attr('main',3)
    })
    //点击时间
    $('.time').toggle(function(){
        stop = 2;
        $(this).html('时间↑');
        var main =  $(this).attr('main');
        $('.arc, .page').remove();
        $('.beforeajax').show();
        startajax(1,main);
        $(this).attr('main',2);
        
    },function(){
        stop = 2;
        $(this).html('时间↓');
        var main =  $(this).attr('main');
        $('.arc, .page').remove();
        $('.beforeajax').show();
        startajax(1,main);
        $(this).attr('main',1)
    })
    //ajax读取页面
    function startajax(num,main){
        $.ajax({
            type:'get',
            'url':url+'/showArc/page/'+num+'/main/'+main,
            dataType:'json',
            beforeSend:function(){
                $('.beforeajax').append(circle.canvas);
                $('.beforeajax').show();
            },
            success:function(str){
                if(str.num==1){
                    var arr = str.arr;
                    var tablestr = '';
                    for(var i=0;i<arr.length;i++){ 
                        tablestr += "<tr class='arc'><td><input type='checkbox' name='id' value="+arr[i].aid+" class='check'></td><td>"+arr[i].aid+"</td><td>"+arr[i].title+"</td><td>"+arr[i].partname+"</td>"
                        tablestr += "<td>"+arr[i].click+"</td>";
                        tablestr += "<td>"+arr[i].time+"</td><td>";
                        tablestr +="&nbsp;<a href='javascript:;' name="+arr[i].aid+" class='delarc'>删除</a> | ";
                        if(arr[i].del==0){
                            tablestr +="<a href='"+ROOT+"/index.php?c=Index&m=arc&aid="+arr[i].aid+"' target='blank'>预览</a> | "
                            tablestr +="<a href='"+url+"/modify&aid="+arr[i].aid+"'>修改</a>";
                        }else{
                            tablestr +="<a href='"+ROOT+"/index.php?c=Index&m=arc&aid="+arr[i].aid+"' target='blank'>查看</a> | ";
                            tablestr +="<a href='javascript:;' class='reduction' name="+arr[i].aid+">还原</a>"; 
                        }
                        tablestr += "</td></tr>";
                    }
                    $('.table1 tr:first').after(tablestr);
                    if(str.page){
                        var page = "<div class='page'>"+str.page+"<a href='javascript:;'>跳转</a></div>";
                        $('.table1 tr td:last').append(page);
                    }
                    $(circle.canvas).remove();
                    $('.beforeajax').hide();
                }else{
                    $(circle.canvas).remove();
                    $('.beforeajax').hide();
                }
            }
        }
        )
    }
   
})


